#include "FiveWin.ch"

#define ES_LEFT          0
#define ES_AUTOHSCROLL 128  // 0x80

//----------------------------------------------------------------------------//

CLASS TGet FROM TControl

   DATA   bSetGet

   METHOD New( nRow, nCol, bSetGet, oWnd, nWidth, nHeight ) CONSTRUCTOR
   METHOD cToChar() BLOCK { | Self | Super:cToChar( "EDIT" ) }
   METHOD ReDefine( nId, bSetGet, oWnd, nHelpId ) CONSTRUCTOR
   METHOD Init( hDlg )
   METHOD SetFocus( bOnOff )
   METHOD cText( cText ) SETGET
   METHOD Refresh()

ENDCLASS

//----------------------------------------------------------------------------//

METHOD New( nRow, nCol, bSetGet, oWnd, nWidth, nHeight ) CLASS TGet

   ::cCaption = cValToChar( Eval( bSetGet ) )
   ::nTop     = nRow * 13
   ::nLeft    = nCol * 8
   ::nBottom  = If( nHeight == nil, ::nTop + 11, ::nTop + nHeight )
   ::nRight   = If( nWidth == nil, ::nLeft + Len( ::cCaption ) * 3.5, ;
                    ::nLeft + nWidth )
   ::bSetGet  = bSetGet
   ::oWnd     = oWnd
   ::nStyle   = nOR( WS_CHILD, WS_VISIBLE, WS_BORDER, WS_TABSTOP, ES_LEFT,;
                     ES_AUTOHSCROLL )
   ::nId      = ::GetNewId()
   ::cCaption = RTrim( ::cCaption )

   if oWnd:ClassName() != "TDIALOG"
      ::Create( "EDIT" )
   else
      oWnd:DefControl( Self )
   endif

return

//----------------------------------------------------------------------------//

METHOD ReDefine( nId, bSetGet, oWnd, nHelpId ) CLASS TGet

   ::nId     = nId
   ::bSetGet = bSetGet
   ::oWnd    = oWnd
   ::nHelpId = nHelpId

   oWnd:DefControl( Self )

return

//----------------------------------------------------------------------------//

METHOD Init( hDlg ) CLASS TGet

   Super:Init( hDlg )
   SetWindowText( ::hWnd, cValToChar( Eval( ::bSetGet ) ) )

return

//----------------------------------------------------------------------------//

METHOD cText( cText ) CLASS TGet

   if PCount() == 1
      SetWindowText( ::hWnd, cText )
   endif

return GetWindowText( ::hWnd )

//----------------------------------------------------------------------------//

METHOD SetFocus( bOnOff ) CLASS TGet

   if bOnOff
      SetWindowText( ::hWnd, cValToChar( Eval( ::bSetGet ) ) )
   else
      Eval( ::bSetGet,;
            uCharToVal( GetWindowText( ::hWnd ), Eval( ::bSetGet ) ) )
   endif

return

//----------------------------------------------------------------------------//

static function cValToChar( uVal )

   local cType   := ValType( uVal )
   local cResult := ""

   do case
      case cType == "C"
           cResult = uVal

      case cType == "N"
           cResult = AllTrim( Str( uVal ) )

      case cType == "D"
           cResult = DToC( uVal )

      case cType == "L"
           cResult = If( uVal, ".T.", ".F." )
   endcase

return cResult

//----------------------------------------------------------------------------//

static function uCharToVal( cValue, uType )

   local cType   := ValType( uType )
   local uResult

   do case
      case cType == "C"
           uResult = cValue

      case cType == "N"
           uResult = Val( cValue )

      case cType == "D"
           uResult = CToD( cValue )

      case cType == "L"
           uResult = ( cValue == ".T." )
   endcase

return uResult

//----------------------------------------------------------------------------//

METHOD Refresh() CLASS TGet

   SetWindowText( ::hWnd, cValToChar( Eval( ::bSetGet ) ) )

return

//----------------------------------------------------------------------------//
